package org.example.service.util.md.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.MdFileContentMapper;
import org.example.model.common.AppHttpCodeEnum;
import org.example.model.common.ResponseResult;
import org.example.model.util.dtos.md.MdFileContentDto;
import org.example.model.util.entities.md.MdFileContent;
import org.example.model.util.vos.md.MdFileContentVo;
import org.example.service.util.md.MdFileContentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * md编辑器文件具体内容服务层实现
 */
@Service
@Transactional
@Slf4j
public class MdFileContentServiceImpl extends ServiceImpl<MdFileContentMapper, MdFileContent> implements MdFileContentService {

    @Autowired
    private MdFileContentMapper mdFileContentMapper;

    /**
     * 添加一条md编辑器文件具体内容
     */
    @Override
    public Boolean InsertMdFileContent(Integer fileId) {
        MdFileContent mdFileContent = new MdFileContent();
        mdFileContent.setFileId(fileId);
        mdFileContent.setContent("");
        mdFileContent.setIsDeleted(false);
        return mdFileContentMapper.insert(mdFileContent) > 0;
    }

    /**
     * 根据id修改一条md编辑器文件具体内容
     */
    @Override
    public ResponseResult<Boolean> UpdateMdFileContentById(MdFileContentVo mdFileContentVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        // 参数审查
        if (mdFileContentVo == null || mdFileContentVo.getId() <= 0) {
            // 缺少参数
            result.error(AppHttpCodeEnum.PARAM_REQUIRE.getCode(), AppHttpCodeEnum.PARAM_REQUIRE.getErrorMessage());
            return result;
        }
        MdFileContent mdFileContent = new MdFileContent();
        BeanUtils.copyProperties(mdFileContentVo, mdFileContent);
        Boolean flag = mdFileContentMapper.updateById(mdFileContent) > 0;
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), flag, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 根据id删除一条md编辑器文件具体内容
     */
    @Override
    public Boolean DeleteMdFileContentById(Integer fileId) {
        MdFileContent mdFileContent = this.HandleGetMdFileContentByFileId(fileId);
        mdFileContent.setIsDeleted(true);
        return mdFileContentMapper.updateById(mdFileContent) > 0;
    }

    /**
     * 根据文件id获取一条md编辑器文件具体内容
     */
    private MdFileContent HandleGetMdFileContentByFileId(Integer fileId) {
        LambdaQueryWrapper<MdFileContent> mdFileContentQuery = new LambdaQueryWrapper<>();
        mdFileContentQuery.eq(MdFileContent::getIsDeleted, false)
                .eq(MdFileContent::getFileId, fileId);
        return mdFileContentMapper.selectOne(mdFileContentQuery);
    }

    /**
     * 根据文件id获取一条md编辑器文件具体内容
     */
    @Override
    public ResponseResult<MdFileContentDto> SelectMdFileContentByFileId(Integer fileId) {
        ResponseResult<MdFileContentDto> result = new ResponseResult<>();
        // 参数审查
        if (fileId <= 0) {
            // 缺少参数
            result.error(AppHttpCodeEnum.PARAM_REQUIRE.getCode(), AppHttpCodeEnum.PARAM_REQUIRE.getErrorMessage());
            return result;
        }
        MdFileContent mdFileContent = this.HandleGetMdFileContentByFileId(fileId);
        MdFileContentDto mdFileContentDto = new MdFileContentDto();
        BeanUtils.copyProperties(mdFileContent, mdFileContentDto);
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), mdFileContentDto, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }
}
